/*
 * Copyright (c) 2010 Curtis Boyden.  All rights reserved. 
 * This software was developed by Curtis Boyden and is provided under the terms 
 * of the GNU Lesser General Public License, Version 2.1. You may not use 
 * this file except in compliance with the license. If you need a copy of the license, 
 * please go to http://www.gnu.org/licenses/lgpl-2.1.txt. The Original Code is ejMud
 * (Ewerp Java MUD).  The Initial Developer is Curtis Boyden.
 *
 * Software distributed under the GNU Lesser Public License is distributed on an "AS IS" 
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or  implied. Please refer to 
 * the license for the specific language governing your rights and limitations.
*/

package com.ewerp.mud.commands.admin;

import org.apache.commons.lang.StringUtils;

import com.ewerp.mud.commands.AccessControlledCommand;
import com.ewerp.mud.commands.ICommand;
import com.ewerp.mud.commands.parsers.StringCommandParser.IStringParseableCommand;
import com.ewerp.mud.core.EwerpMud;
import com.ewerp.mud.core.exceptions.EwerpMudException;
import com.ewerp.mud.entitycore.descriptions.BaseDescription;
import com.ewerp.mud.entitycore.descriptions.meta.VisibleDescription;
import com.ewerp.mud.locations.rooms.BaseRoom;
import com.ewerp.mud.locations.rooms.IRoom;
import com.ewerp.mud.locations.rooms.IRoomId;
import com.ewerp.mud.locations.rooms.IRoomManager;
import com.ewerp.mud.locations.rooms.PropertyStartingRoom;
import com.ewerp.mud.locations.rooms.RoomId;
import com.ewerp.mud.security.permissions.IPermission;

public class CreateRoomCommand extends AccessControlledCommand implements IStringParseableCommand {

    protected String description;
    protected String name;
    protected boolean isStartRoom;
    
    @Override
    public void doExecute() {
        IRoom newRoom = new BaseRoom();
        newRoom.setDescription(new BaseDescription(new VisibleDescription(), getDescription()));
                
        IRoomId roomId = new RoomId();
        roomId.setName(getName());
        
        newRoom.setId(roomId);
        
        if(isStartRoom()) {
        	PropertyStartingRoom startRoom = new PropertyStartingRoom();
        	newRoom.getProperties().add(startRoom);
        }
        
        try {
            ((IRoomManager)pluginManager.getPlugin(EwerpMud.defaultNamespace, IRoomManager.class)).addRoom(newRoom);
        } catch (EwerpMudException e) {
            e.printStackTrace();
        }
    }

    @Override
    public IPermission[] getPermissions() {
        return AccessControlledCommand.STANDARD_ADMIN_ROLE;
    }
    
    public String getDescription() {
        return description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public boolean isStartRoom() {
    	return isStartRoom;
    }
    
    public void setStartRoom(boolean isStartRoom) {
    	this.isStartRoom = isStartRoom;
    }

	@Override
	public ICommand parseString(String input) {
		CreateRoomCommand cmd = new CreateRoomCommand();
		if(!StringUtils.isEmpty(input)) {
			if(input.startsWith("(start)")) {
				setStartRoom(true);
			}
			cmd.setName(input);
		}
        cmd.setDescription("A bare room");
        
        return cmd;
	}

}
